package com.alin.swing.component.base;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * @ClassName J2JButtonMain
 * @Description: 按钮：JButton，按钮，就是点击的按钮
 * @Author chenlin
 * @Date 2021/6/7 14:52
 * @Version V1.0
 **/
public class J2JButtonMain {
    public J2JButtonMain(){
        JFrame jf = new JFrame("测试窗口");
        jf.setSize(200, 200);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();

        // 创建一个按钮
        final JButton btn = new JButton("测试按钮");

        // 添加按钮的点击事件监听器
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取到的事件源就是按钮本身
                // JButton btn = (JButton) e.getSource();

                System.out.println("按钮被点击");
            }
        });

        // 创建一个按钮
        final JButton btnLz = new JButton("靓仔按钮");

        // 添加按钮的点击事件监听器
        btnLz.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取到的事件源就是按钮本身
                // JButton btn = (JButton) e.getSource();

                System.out.println("靓仔按钮被点击");
            }
        });

        panel.add(btn);
        panel.add(btnLz);
        jf.setContentPane(panel);
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        new J2JButtonMain();
    }
}
